<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var insertIntoBST = function (root, val) {
        if (!root) return new TreeNode(val)
        let parentNode = null
        function preOrder(root) {
          if (!root) {
            //找到叶子节点，只需要按照大小插入到叶子节点左右即可，不需要重构二叉树
            let node = new TreeNode(val)
            if (val < parentNode.val) {
              parentNode.left = node
            } else {
              parentNode.right = node
            }
            return
          }
          parentNode = root
          if (root.val < val) {
            preOrder(root.right)
          }
          if (root.val > val) {
            preOrder(root.left)
          }
        }
        preOrder(root)
        return root
      }
    </script>
  </body>
</html>
